import 'package:flutter/material.dart';
import 'package:flutter_common/flutter_common.dart';

typedef FxPopupMenuItemBuilder<T> = List<FxPopupItem<T>> Function(
    BuildContext context);

typedef FxPopupMenuItemSelected<T> = void Function(T value);

///button for show popup menu
class FxPopupMenuButton<T> extends StatelessWidget {
  final FxPopupMenuItemBuilder<T> builder;
  final FxPopupMenuItemSelected<T>? onSelected;
  final Offset? offset;
  final Widget? icon;
  final Widget? child;

  const FxPopupMenuButton(
      {Key? key,
      required this.builder,
      this.onSelected,
      this.offset,
      this.icon = const Icon(
        Icons.more_vert_rounded,
        color: Color(0xFFBBBBBB),
        size: 20,
      ),
      this.child})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return PopupMenuButton<T>(
      padding: EdgeInsets.zero,
      offset: offset ?? const Offset(0, 0),
      icon: icon,
      onSelected: onSelected,
      itemBuilder: (BuildContext context) =>
          builder.call(context).map((e) => FxPopupMenuItem<T>(e)).toList(),
      child: child,
    );
  }
}

///fa xin‘s popup menu的显示item
class FxPopupMenuItem<T> extends PopupMenuItem<T> {
  final FxPopupItem<T> item;

  FxPopupMenuItem(this.item, {Key? key})
      : super(
          key: key,
          height: 36,
          value: item.value,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            mainAxisSize: MainAxisSize.min,
            children: [
              if (item.assetsName != null)
                Image.asset(
                  item.assetsName!,
                  width: 15,
                  height: 15,
                ),
              if(item.assetsIcon != null) item.assetsIcon!,
              const SizedBox(width: 6),
              Text(
                item.name,
                style: TextStyle(color: FxTheme.instance.textColor),
              )
            ],
          ),
        );
}

///popup item bean
class FxPopupItem<T> {
  final String? assetsName;
  final Icon? assetsIcon;
  final String name;
  final T value;

  FxPopupItem({this.assetsName, this.assetsIcon, required this.name, required this.value});
}
